<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="icon" type="shortcut icon" th:href="@{/img/favicon.ico}" />
	<link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}" />
	<link rel="stylesheet" type="text/css" th:href="@{/css/global.css}" />
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">
	
	<link rel="stylesheet" type="text/css" th:href="@{/editor-md/examples/css/style.css}" />
	<link rel="stylesheet" type="text/css" th:href="@{/editor-md/css/editormd.css}" />
	
	<title>郑在守护—编辑帖子</title>
	<style>
		/* 防诈骗横幅样式 */
		.anti-fraud-banner {
			background: rgb(18, 127, 249);
			color: white;
			padding: 8px;
			text-align: center;
			font-size: 0.9rem;
		}
		
		.anti-fraud-banner h4 {
			font-size: 1rem;
			margin-bottom: 4px;
		}
		
		.anti-fraud-banner p {
			font-size: 0.8rem;
			margin-bottom: 0;
		}
		
		/* 确保header不是深色背景 */
		header.bg-dark {
			background-color: white !important;
		}
		
		/* 直接复制index.html的导航栏样式 */
		.navbar {
			background-color: white !important;
			box-shadow: 0 1px 3px rgba(0,0,0,0.1);
			height: 56px;
			padding: 0;
			border-radius: 4px;
			margin-top: 5px;
		}
		
		.navbar-brand {
			display: flex;
			align-items: center;
			padding: 0 16px;
			height: 56px;
			border-right: 1px solid #f0f0f0;
			font-weight: 600;
			color: #2196F3;
			transition: all 0.3s ease;
		}
		
		.navbar-brand:hover {
			background-color: rgba(33, 150, 243, 0.05);
		}
		
		.navbar-brand i {
			color: #2196F3;
			font-size: 20px;
			margin-right: 8px;
		}
		
		.navbar-brand span {
			color: #2196F3;
			font-size: 16px;
			font-weight: 600;
		}
		
		.navbar-nav {
			display: flex;
			align-items: center;
			height: 56px;
			padding: 0 12px;
		}
		
		.nav-item {
			height: 56px;
			margin: 0 2px;
		}
		
		.nav-link {
			display: flex !important;
			align-items: center;
			height: 56px;
			padding: 0 12px !important;
			color: #666 !important;
			position: relative;
			font-size: 13px;
			transition: all 0.3s ease;
		}
		
		.nav-link i {
			font-size: 16px;
			margin-right: 5px;
			transition: transform 0.2s ease;
			display: inline-block;
			vertical-align: middle;
		}
		
		.nav-link span {
			display: inline-block;
			vertical-align: middle;
		}
		
		.nav-link:hover {
			color: #2196F3 !important;
			background-color: rgba(33, 150, 243, 0.05);
		}
		
		.nav-link:hover i {
			transform: scale(1.1);
		}
		
		.nav-link.active {
			color: #2196F3 !important;
			font-weight: 500;
		}
		
		.nav-link.active:after {
			content: '';
			position: absolute;
			bottom: 0;
			left: 16px;
			right: 16px;
			height: 2px;
			background-color: #2196F3;
		}

		/* 修改通知徽章样式，使其成为红色圆圈白色数字 */
		.badge-danger {
			position: absolute;
			top: -5px;
			right: -5px;
			padding: 0.25rem 0.4rem;
			font-size: 0.75rem;
			font-weight: 700;
			color: #fff;
			background-color: #ff4d4f;
			border-radius: 50%;
			min-width: 18px;
			height: 18px;
			line-height: 1;
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			box-shadow: 0 0 0 1px #fff;
		}

		/* 为导航项添加相对定位，以便正确放置通知徽章 */
		.nav-link.position-relative {
			position: relative;
		}
		
		.search-form {
			position: relative;
			margin-right: 12px;
		}
		
		.search-form input {
			width: 200px;
			height: 32px;
			padding: 0 32px 0 12px;
			border: 1px solid #e8e8e8;
			border-radius: 16px;
			background-color: #f5f5f5;
			font-size: 13px;
			transition: all 0.3s ease;
		}
		
		.search-form input:focus {
			width: 240px;
			border-color: #2196F3;
			box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.2);
			outline: none;
		}
		
		.search-form .serach-btn {
			position: absolute;
			right: 4px;
			top: 4px;
			width: 24px;
			height: 24px;
			font-size: 14px;
			background-color: transparent;
			border: none;
			color: #2196F3;
			cursor: pointer;
			transition: all 0.2s ease;
		}
		
		.search-form .serach-btn:hover {
			transform: scale(1.1);
			color: #1976D2;
		}
		
		.user-info {
			height: 56px;
			border-left: 1px solid #f0f0f0;
			padding-left: 16px;
			transition: all 0.3s ease;
		}
		
		.user-info:hover {
			background-color: rgba(33, 150, 243, 0.05);
		}
		
		.user-avatar {
			width: 32px;
			height: 32px;
			border-radius: 50%;
			margin-right: 6px;
			border: 2px solid rgba(33, 150, 243, 0.2);
			transition: all 0.3s ease;
		}
		
		.user-info:hover .user-avatar {
			border-color: rgba(33, 150, 243, 0.5);
		}
		
		/* 确保导航栏水平显示 */
		.navbar-nav.mr-auto {
			flex-wrap: nowrap;
			overflow-x: visible;
			width: auto;
			margin-right: 0 !important;
		}
		
		/* 确保导航栏容器宽度合适 */
		.navbar > .container {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 10px;
			max-width: 100%;
		}
		
		/* 调整折叠导航栏 */
		.collapse.navbar-collapse {
			display: flex !important;
			flex-basis: auto;
			justify-content: space-between;
		}
		
		/* 确保导航项目在一行显示 */
		.navbar-nav {
			flex-direction: row !important;
			flex-wrap: nowrap !important;
		}
		
		/* 调整导航项目间距 */
		.navbar-nav .nav-item {
			white-space: nowrap;
			margin: 0 2px;
		}
		
		/* 调整搜索框和用户信息的位置 */
		.navbar-right {
			display: flex;
			align-items: center;
			margin-left: auto;
		}
		
		/* 添加额外的样式确保导航栏在一行显示 */
		@media (min-width: 992px) {
			.navbar-expand-lg .navbar-nav {
				flex-direction: row;
			}
			
			.navbar-expand-lg .navbar-collapse {
				display: flex !important;
				flex-basis: auto;
			}
		}
		
		/* 调整编辑器布局 */
		#layout {
			margin-top: 20px;
		}
	</style>
</head>
<body>
	<div class="nk-container">
		<!-- 防诈骗横幅 -->
		<div class="anti-fraud-banner">
			<div class="container">
				<h4><i class="bi bi-shield-check"></i> 提高警惕，防范诈骗</h4>
				<p class="mb-0">共建平安校园，远离电信诈骗</p>
			</div>
		</div>
		
		<!-- 头部 -->
		<header class="bg-white sticky-top" th:replace="index::header"></header>

		<!-- 内容 -->
		<div id="layout">
			<header>
				<div class="form-group">
					<input type="text" class="form-control" style="font-size: 24px; font-weight: 500;"
						   id="recipient-name" name="title" th:value="${post.title}" placeholder="输入文章标题..." required>
				</div>
			</header>

			<div id="test-editormd">
				<textarea style="display:none;" name="content" th:text="${post.content}"></textarea>
			</div>

			<div style="text-align: center">
				<button type="button" class="btn btn-outline-secondary" id="backIndexBtn">返回首页</button>
				<button type="button" class="btn btn-outline-primary" id="updateBtn"
						style="color: rgb(119, 84, 223)">保存修改</button>
			</div>

			<!-- 提示框 -->
			<div class="modal fade" id="hintModal" tabindex="-1" role="dialog" aria-labelledby="hintModalLabel" aria-hidden="true">
				<div class="modal-dialog modal-lg" role="document">
					<div class="modal-content">
						<div class="modal-header">
							<h5 class="modal-title" id="hintModalLabel">提示</h5>
						</div>
						<div class="modal-body" id="hintBody"></div>
					</div>
				</div>
			</div>
		</div>

		<!-- 移除页脚 -->
		<!-- 原来的页脚代码被移除了 -->
	</div>

	<script th:src="@{/js/jquery-3.1.0.min.js}"></script>
	<script th:src="@{/editor-md/editormd.min.js}"></script>
	<script th:src="@{/js/popper.min.js}"></script>
	<script th:src="@{/js/bootstrap.min.js}"></script>
	<script th:src="@{/js/global.js}"></script>
	
	<script type="text/javascript">
		var testEditor;
		
		$(function() {
			testEditor = editormd("test-editormd", {
				width   : "90%",
				height  : 640,
				syncScrolling : "single",
				path    : "/editor-md/lib/",  // 修改这里，使用绝对路径
				saveHTMLToTextarea : true, // 这个配置，方便 post 提交表单
				
				/**上传图片相关配置如下*/
				imageUpload : true,
				imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
				imageUploadURL : CONTEXT_PATH + "/discuss/uploadMdPic",// 后端上传图片的服务地址
				onload : function() {
					console.log('编辑器加载完成');  // 添加日志以便调试
					// 确保内容正确加载
					if (testEditor && testEditor.getMarkdown) {
						console.log('内容长度:', testEditor.getMarkdown().length);
					}
				}
			});
			
			// 返回首页按钮点击事件
			$("#backIndexBtn").click(function(){
				window.location.href = CONTEXT_PATH + "/index";
			});
			
			// 保存修改按钮点击事件
			$("#updateBtn").click(function(){
				var postId = [[${post.id}]];
				var title = $("#recipient-name").val();
				var content = testEditor.getMarkdown();
				
				// 添加简单验证
				if (!title || title.trim() === '') {
					$("#hintBody").text("标题不能为空!");
					$("#hintModal").modal("show");
					return;
				}
				
				$.ajax({
					url: CONTEXT_PATH + "/discuss/update/" + postId,
					type: "post",
					data: {"title":title, "content":content},
					dataType: "json",
					success: function(data) {
						if(data.code == 0) {
							$("#hintBody").text("修改成功!");
							$("#hintModal").modal("show");
							setTimeout(function(){
								$("#hintModal").modal("hide");
								window.location.href = CONTEXT_PATH + "/discuss/detail/" + postId;
							}, 2000);
						} else {
							$("#hintBody").text(data.msg);
							$("#hintModal").modal("show");
							setTimeout(function(){
								$("#hintModal").modal("hide");
							}, 2000);
						}
					},
					error: function(xhr) {
						// 添加错误处理
						console.error("更新失败:", xhr);
						$("#hintBody").text("更新失败，请稍后再试!");
						$("#hintModal").modal("show");
					}
				});
			});
		});
	</script>
</body>
</html>